//
//  TransferFunction3D.cpp
//  
//
//  Created by Kun Zhao on 12/12/28.
//  Copyright (c) 2012 Kyoto University. All rights reserved.
//

#include <iostream>
#include "TransferFunction3D.h"

namespace kun
{

TransferFunction3D::TransferFunction3D( void )
{
}
    
TransferFunction3D::TransferFunction3D( const std::string filename )
{    
    this->import( filename );
}
    
void TransferFunction3D::setResolution( kvs::Vector3f resolution )
{
    m_resolution = resolution;
}

kvs::Vector3f TransferFunction3D::resolution( void )
{
    return( m_resolution );
}
    
void TransferFunction3D::setValues( float* values )
{
    m_data = values;
}
    
float* TransferFunction3D::values( void )
{
    return( m_data );        
}
    
void TransferFunction3D::setMinMaxVelocity( float min, float max )
{
    m_min_velocity = min;
    m_max_velocity = max;
}

float TransferFunction3D::min_velocity( void )
{
    return( m_min_velocity );
}
    
float TransferFunction3D::max_velocity( void )
{
    return( m_max_velocity );
}
    
const bool TransferFunction3D::import( const std::string filename )
{
    FILE *pFile = fopen( filename.c_str(), "rb" );
    fseek( pFile, 0, SEEK_END );
    size_t fl = ftell( pFile ) / 4;
    rewind( pFile );
    
    float* buf = new float[fl];
    fread( buf, sizeof( float ), fl, pFile);
    fclose( pFile ) ;
    
    m_resolution.x() = buf[0];
    m_resolution.y() = buf[1];
    m_resolution.z() = buf[2];
    size_t length = m_resolution.x() * m_resolution.y() * m_resolution.z() * 4;

    m_data = new float[ length ];
    for ( size_t i = 0; i < length; i++ )
    {
        m_data[i] = buf[i + 3];
    }
    
    return( true );
}
    
const bool TransferFunction3D::write( const std::string filename )
{
    FILE * pFile;
    pFile = fopen ( filename.c_str(), "wb" );
    size_t fl = m_resolution.x() * m_resolution.y() * m_resolution.z() * 4;
    fwrite ( &m_resolution.x(), sizeof( float ), 1, pFile );
    fwrite ( &m_resolution.y(), sizeof( float ), 1, pFile );
    fwrite ( &m_resolution.z(), sizeof( float ), 1, pFile );
    fwrite ( m_data, sizeof( float ), fl, pFile );
    fclose (pFile);
    return( true );
}

}
